home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 8 / Commodore_Free_Issue_08_2007_Commodore_Computer_Club.d64 / t.ls prg part ii < prev    next >
Text File  |  2023-02-26  |  12KB  |  491 lines

  1. uMr. LOADSTAR's Intro to Programming
  2. the C64 Part II By Dave Moorman
  3.  
  4. Now you have certainly played around
  5. with the PRINT statement. The question
  6. arises -- how can I put the text
  7. exactly where I want it on the screen?
  8. We have several ways. You might have
  9. figured out the first. Use embedded
  10. cursor keystrokes. This is easy
  11.  
  12. 10 ?[clr][down][down][right][right]
  13.  [right]Test
  14.  
  15. And it looks like a mess in your
  16. program, with all those reversed
  17. characters. There are two better ways.
  18.  
  19. The first is a routine available from
  20. the Kernal ROM. These routines are what
  21. BASIC uses to do its magic. But we can
  22. use them directly with a couple of new
  23. (to you) commands: POKE & SYS. NEW your
  24. memory & try this:
  25.  
  26. 5 ?"[clr]"
  27. 10 x=782:y=781:set=783:plot=65520
  28. 20 poke x,10:poke y,10:poke set,0
  29. 30 sys plot:?"This can be centered"
  30.  
  31. PLOT is a Machine Language routine. We
  32. use POKE (you can use a shortcut of
  33. "pO" to save typing) to put the X
  34. column in memory location 782 & the Y
  35. row in location 781. SET (783) must be
  36. set to 0 for this to work. Then the SYS
  37. command executes ML code at 65520. You
  38. will notice that we can put more than
  39. one command on a program line,
  40. separated by colons. Actually, you have
  41. two screen lines available for each
  42. program line. Back to plotting text on
  43. the screen. The third way is to use a
  44. poke & the TAB command.
  45.  
  46. 50 row=15:col=10
  47. 60 poke 214,row-1
  48. 70 ?tab(col)"This can be centered"
  49.  
  50. The key here is the PRINT after poking
  51. ROW-1 in location 214. If you want to
  52. put your text on the top line, use
  53. PRINT"[home]".
  54.  
  55. Either way of plotting is just fine --
  56. you choose.
  57.  
  58. LOOPING THE LOOP
  59.  
  60. While all this is fun, the greatest
  61. power of a computer is to do things
  62. over & over again. Repetition makes the
  63. world go round! NEW your memory & type
  64. in this simple code.
  65.  
  66. 10 ?"[clr]"
  67. 20 ?"Hello, World"
  68. 30 goto 20
  69.  
  70. Before you run this, try to figure out
  71. what will happen. The GOTO command is
  72. new, but it is fairly obvious. Now, run
  73. the program.
  74.  
  75. WHOA! Look at it go! When will it stop?
  76.  
  77. It won't. This is called an "infinite
  78. loop," since it will infinitely print
  79. "Hello, World!" on line 20, then goto
  80. line 20, where it prints but you get
  81. the idea. In fact, you may be looking
  82. at it in action, wondering what to do.
  83. Since you are programming in BASIC, the
  84. answer is simple. Press [STOP]. The
  85. result is not particularly elegant,
  86. what with
  87.  
  88. break in 20
  89. ready.
  90.  
  91. appearing on the screen. But this IS an
  92. infinite loop. Be happy we have a
  93. [STOP] key! (Have you ever had your
  94. Windows PC "hang?" No combination of
  95. keys, not even the [CTRL-ALT-DELETE]
  96. will get back control. Guess what kind
  97. of error some programmer made! Yep --
  98. an infinite loop -- with no way for the
  99. user to get out. That can happen in ML.
  100. Infinity is great, but we need more
  101. control. More POWER! We need to be able
  102. to stop the loop when we want it to
  103. stop -- when the conditions are right.
  104.  
  105. Behold! The "conditional loop!"
  106.  
  107. 10 ?"[clr]"
  108. 15 x=0
  109. 20 ?"Hello, World!"
  110. 30 x=x+1
  111. 40 if x<10 then 20
  112. 50 end
  113.  
  114. Here, we use X as a counter. You do not
  115. have to zero out X as in line 15, but
  116. it is a good idea. You never know where
  117. that X has been! Line 20 prints the
  118. text, as before. Then line 30
  119. increments X. Now, if you are not
  120. familiar with programming, saying X=X+1
  121. sounds a little crazy, since in
  122. algebra, X can never equal X+1. But the
  123. equal sign here is not equal. It is the
  124. sign to assign X with the value of X+1.
  125. Long ago, even before home computers,
  126. BASIC had a command for this. Change
  127. line 30 to
  128.  
  129. 30 let x=x+1
  130.  
  131. Now, as you read this out loud, the
  132. equal sign makes more sense, since LET
  133. lets X change its value. Think of it
  134. this way. X is a box, right? Inside
  135. that box, at the beginning of the
  136. program is a value of nothing. So, we
  137. take the nothing out of the box, add
  138. one to it, & put the result back in the
  139. box. The program loops, & now X
  140. contains 1. We take out the 1, add 1, &
  141. put the result back into the box. This
  142. is exactly what happens with any value
  143. assignment. You don't need the LET
  144. command, so don't waste your time or
  145. your computer's memory. Whenever a
  146. variable is followed by an equal sign,
  147. the genie knows an assignment is about
  148. to happen. Now that you understand
  149. incrementing, lets look at line 40.
  150. This is the IF-THEN command. The genie
  151. looks at the comparison following the
  152. IF. In this case it is X<10. That's "X
  153. is less than 10." If that is true, the
  154. THEN happens -- in this case, the
  155. program goes to line 20. If X is not
  156. less than 10, the IF-THEN is said to
  157. "fall through," & the next program line
  158. is executed.
  159.  
  160. Any two numeric values (variables or
  161. constants) or any two strings
  162. (variables or literals) can be compared
  163. this way, using one of these
  164. comparisons:
  165.  
  166. Equals =
  167. Greater Than >
  168. Less Than <
  169. Greater or Equal => or >=
  170. Less or Equal <= or =<
  171. Not Equal <>
  172.  
  173. The IF-THEN command is what gives the
  174. computer its intelligence. You might
  175. have noticed that the genie is not too
  176. bright. It does exactly what you tell
  177. it to -- if it understands what you
  178. mean. (I will bet you have suffered a
  179. lot of SYNTAX  ERRORs!) So with the
  180. IF-THEN command, we tell the genie to
  181. change the flow of the program when a
  182. certain condition applies. And counting
  183. is just one possible condition. Here is
  184. another
  185.  
  186. 40 if peek(198)=0 then 20
  187. 45 poke198,0
  188.  
  189. Here we are using another "system
  190. resource," a location in memory that
  191. BASIC uses for its own purposes.
  192. Location 198 holds how many times a key
  193. has been pressed since the last time
  194. keypresses were collected by the
  195. system. And, how about that -- we have
  196. encountered another command. PEEK(loc)
  197. peeks under BASIC right into a memory
  198. location itself. And we are fortunate
  199. to have PEEK, POKE, & SYS, because the
  200. C64 has a lot more power than BASIC 2.0
  201. can handle.
  202.  
  203. In this case, we are looking at the
  204. keyboard queue, the number of
  205. keystrokes waiting to be processed. If
  206. 0, then the program loops. If not, we
  207. POKE a 0 into 198 (to clean things up a
  208. bit), & end the program. I probably
  209. should have put 198 in a variable
  210.  
  211. 16 key=198
  212.  
  213. 40 if peek(key)=0 then 20
  214. 45 poke key,0
  215.  
  216. Looks much nicer, eh? However, as you
  217. learn your way around the C64, you will
  218. discover 198 is one of those locations
  219. you will naturally learn by heart.
  220.  
  221. As with all programming, we have more
  222. than one way to do most anything. That
  223. is part of the fun -- finding the best
  224. way -- for speed and/or elegance -- to
  225. accomplish a goal. Time for another
  226. tiny program, so NEW & enter:
  227.  
  228. 10 ?"[clr]"
  229. 20 ?"Hello, World!"
  230. 30 getz$
  231. 40 if z$="" then 20
  232.  
  233. The GET command gets a keystroke, if
  234. any, & puts it in the string variable
  235. you designate. I always use Z$. It is a
  236. habit of mine. In fact, I use Z$ for
  237. nothing else.The comparison in line 40
  238. is to see if Z$ holds anything -- or
  239. rather, if it is equal to nothing,
  240. which is indicated by 2 double-quotes.
  241. As long as it holds nothing, the
  242. program loops. This is more elegant,
  243. than the PEEK(198) -- & full of
  244. possibilities.
  245.  
  246. Because we can string strings together.
  247. Did I mention "concatenation?" It is a
  248. powerful ability of BASIC. Here is yet
  249. another small program to try. I do hope
  250. you are typing these in, looking them
  251. over, running them, then listing &
  252. looking at them again. I know I am,
  253. even as I write.
  254.  
  255. 10 ?"[clr]"
  256. 20 w$="":c$="<"
  257. 30 ?"[home]"w$c$
  258. 40 getz$:if z$="" then30
  259. 50 if z$=chr$(13) then end
  260. 60 w$=w$+z$
  261. 70 goto30
  262.  
  263. You just wrote your first attempt at a
  264. word processor! It's not a Good word
  265. processor. In fact, as you play with
  266. it, you might notice it's not even a
  267. good input routine. You will notice
  268. this most if you try using [Delete] or
  269. a cursor keystroke. The displayed text
  270. gets all messy.
  271.  
  272. But lets look at what we have done. We
  273. put nothing ("") in W$, & a little
  274. pointer ("<") in c$. Then we print them
  275. on the home row in line 30. Notice, you
  276. don't have to use semi-colons between
  277. string variables. The $ tells the
  278. computer where each variable ends.
  279.  
  280. Then we get Z$. If it is empty (called
  281. "null"), we loop back to the same line
  282. number. OOPS! We loop to the print
  283. line. Change line 40 to go to line 40.
  284. It is neater that way. If a key has
  285. been pressed, line 60 adds
  286. (concatenates) Z$ to the end of W$.
  287. (What would happen if you used
  288. W$=Z$+W$? Then the program loops.
  289.  
  290. Now line 50 is interesting. We must
  291. check Z$ for a [RETURN] key press. But
  292. we cannot use a [RETURN] in the program
  293. line, because it will enter the line
  294. into memory. So we must use a CHR$(n),
  295. which turns a number into a character
  296. string. You might try this:
  297.  
  298. ?chr$(65)
  299.  
  300. You should see an "a" printed on the
  301. screen. Every character has a number.
  302. In fact, inside the computer, there are
  303. no characters -- only numbers. CHR$(13)
  304. is the RETURN character. This is
  305. another number you will memorize.
  306.  
  307. So, if Z$ holds a RETURN, then the
  308. program ends. That simple.
  309.  
  310. But how do we get rid of those things
  311. that mess up the printing of our line?
  312. If you guessed, "Using IF-THEN
  313. commands," you are right. Now to figure
  314. out what numbers to put in such
  315. commands.
  316.  
  317. Add these two lines to the top of your
  318. program:
  319.  
  320. 1 getz$:ifz$="" then1
  321. 2 ?asc(z$):goto1
  322.  
  323. ASC($) returns the number you put in
  324. CHR$(n) to get the character. So press
  325. [a]. Yep -- 65. Lets try several other
  326. characters we do not want in W$.
  327.  
  328. Delete 20
  329. Insert 148
  330. Cursor Up 145
  331. Cursor Dn 17
  332. Cursor Lft 157
  333. Cursor Rt 29
  334. Home 19
  335. CLR 147
  336.  
  337. COLORS:
  338. Black 144
  339. White 5
  340. Red 28
  341. Cyan 159
  342. Purple 156
  343. Green 30
  344. Blue 31
  345. Yellow 158
  346. Orange 129
  347. Brown 149
  348. Lt Red 150
  349. Dk Gray 151
  350. Med Gray 152
  351. Lt Green 153
  352. Lt Blue 154
  353. Lt Gray 155
  354.  
  355. Now, that is quite a list -- & doing an
  356. IF-THEN for each one would take a lot
  357. of lines -- & a lot of time. But
  358. perhaps you see a pattern here? These
  359. numbers fall into two ranges: 5 - 31 &
  360. 144 - 159. So we can eliminate these
  361. keystrokes with 2 IF-THEN
  362. commands. First, break out of this loop
  363. by pressing [STOP]. Then type
  364.  
  365. 1 [RETURN]
  366. 2 [RETURN]
  367.  
  368. That is all you need to do to remove a
  369. program line -- enter its line number.
  370.  
  371. Now add these two lines:
  372.  
  373. 54 z=asc(z$)
  374. 55 if z>=5 & z<=31 then 40
  375. 56 if z>=144 & z<=159 then 40
  376.  
  377. List your program & follow the logic.
  378. We have used ASC(z$) to get the
  379. character value (called the ASCII
  380. value) of the key press. Then we see if
  381. Z is Greater Than or Equal To 5 AND Z
  382. is Less Than or Equal To 31. The AND
  383. means that both conditions must be true
  384. for the IF to be true. Imagine Z= 29.
  385. Is 29 Greater or Equal to 5? YES.Is 29
  386. Less or Equal to 31? YES. Then 29 is in
  387. the range of 5 - 31 -- & we loop back
  388. up to line 40. The same works in line
  389. 56 for the higher range.
  390.  
  391. AND is a Logic Operator, & works like:
  392.  
  393.  A  AND  B  Result
  394. False  False False
  395. False  True  False
  396. True   False False
  397. True   Ture  True
  398.  
  399. So, only when A AND B are True, then
  400. the Result is True.We have another
  401. Logic Operator we can use in IF-THEN
  402. commands: OR
  403.  
  404. A   OR   B  Result
  405.  
  406. False  False False
  407. False  True  True
  408. True   False True
  409. True   True  True
  410.  
  411. If either A OR B (or both) are True,
  412. then the Result is True.
  413.  
  414. Now with this, we can put both IF-THENs
  415. into one:
  416.  
  417. 55 IF (z>=5 AND z<=31) or (z>=144 AND
  418.  Z<=59) THEN 40
  419.  
  420. As with math, the comparisons are done
  421. in the parentheses first. So if Z is in
  422. either range, then the program loops.
  423.  
  424. Now -- that may have you scratching
  425. your head. Don't worry. Many concepts
  426. take time to take root. So, while we
  427. are on the subject, let's look at
  428. exactly happens in a comparison.
  429.  
  430. 1 a = 7
  431. 2 ?a<10
  432. 3 ?a>10
  433. 4 end
  434.  
  435. Run this dab of code. You should get
  436.  
  437. -1
  438.  0
  439.  
  440. In BASIC on the C64, true is -1 & false
  441. if 0. And if you must know, the IF-THEN
  442. command only worries about false. Add
  443. these lines:
  444.  
  445. 4 if 0 then ?"False"
  446. 5 if 1 then ?"True"
  447. 6 stop
  448.  
  449. You will see that checking for a 0 in a
  450. variable is very easy.  These two lines
  451. do exactly the same thing:
  452.  
  453. if a<>0 then 50000
  454.  
  455. if a then 50000
  456.  
  457. But if I confused you, I do apologize.
  458.  
  459. Or better yet, PLAY with it. We have
  460. covered a lot of ground in this
  461. section. We have given you almost
  462. enough to write an arcade game!
  463.  
  464. You know how to print.
  465. You know how to position your printing
  466.  on the screen. (sys plot)
  467. You know how to get keystrokes. (getz$)
  468. You know how to do math with numeric
  469.  variables.
  470. You know how to use IF-THEN commands to
  471.  add intelligence to your computer.
  472. What is left?
  473.  
  474. Lots of stuff! But you have the
  475. essentials. Try to use the cursor keys
  476. to move a dot around the screen. Here
  477. is a good piece of code:
  478.  
  479. 150 getz$:ifz$=""then 150
  480. 160 ifz$="[left]" then x=x-1
  481. 161 ifz$="[right]" then x=x+1
  482. 162 ifz$="[up]" then y=y-1
  483. 163 ifz$="[down]" then y=y+1
  484.  
  485.  
  486. That should get you started
  487.  
  488. The more you fiddle around with these
  489. ideas, the more you will be ready for
  490. our next lesson.
  491.